var express = require('express');
var router = express.Router();
var pool = require("../db/mysql");
var getCode = require("../util/codeUtil");
var dateFormat = require("../util/dateUtil");

const initAward = 30;
const inviteAward = 10;

/* GET users listing. */
router.post('/add', function (req, res, next) {
  var code = req.body.code;
  var cookieCode = req.cookies.text;
  if (!cookieCode) {
    var jsonData = { code: 1, msg: "参数缺失" };
    res.jsonp(jsonData);
    res.end();
    return;
  } else {
    if (cookieCode.toUpperCase() !== code.toUpperCase()) {
      var jsonData = { code: 1, msg: "验证码错误" };
      res.jsonp(jsonData);
      res.end();
      return;
    }
  }
  var award = 0;
  var inviteName = null;
  var eth = req.body.eth;
  var wechatName = req.body.wechatName;
  var inviteCode = req.body.inviteCode;
  var selfInviteCode = getCode(6);
  var date = new Date();
  if (eth && wechatName && selfInviteCode) {
    var insertData = { ETH_ADDRESS: eth, WECHAT_NAME: wechatName, INVITE_CODE: selfInviteCode, CREATE_TIME: date, AWARD_NUM: initAward };

    var queryEthSql = "select INVITE_CODE from base_invite where ETH_ADDRESS ='" + eth + "'";
    var queryEthFun = (resolve, reject) => {
      pool.query(queryEthSql, function (error, results, fields) {
        if (error) reject(error);
        if (results && results[0]) {
          resolve(results[0].INVITE_CODE);
        } else {
          resolve(null);
        }
      });
    }

    var querySql = "select WECHAT_NAME,AWARD_NUM from base_invite where INVITE_CODE ='" + inviteCode + "'";
    var queryFun = (resolve, reject) => {
      pool.query(querySql, function (error, results, fields) {
        if (error) resolve(null);
        if (results && results[0]) {
          resolve(results[0]);
        } else {
          resolve(null);
        }
      });
    }

    var updateFun = (resolve, reject) => {
      var updateSql = "update base_invite set AWARD_NUM = " + award + " where INVITE_CODE ='" + inviteCode + "'";
      pool.query(updateSql, function (error, results, fields) {
        if (error) resolve(null);
        if (results) {
          resolve(null);
        } else {
          resolve(null);
        }
      });
    }

    var sqlFun = (resolve, reject) => {
      pool.query('INSERT INTO base_invite SET ?', insertData, function (error, results, fields) {
        if (error) reject(error);
        resolve(results)
      });
    }
    new Promise(queryEthFun).then((data) => {
      if (data) {
        var jsonData = { code: 0, msg: "", data: data };
        res.jsonp(jsonData);
        res.end();
        return Promise.reject(null);
      } else {
        return new Promise(queryFun);
      }
    }).then((data) => {
      inviteCode = data.WECHAT_NAME;
      award = data.AWARD_NUM;
      award += inviteAward;
      return new Promise(updateFun);
    }).then(data => {
      if (inviteCode) {
        insertData.PARENT_INVITE_CODE = inviteCode;
        insertData.PARENT_INVITE_WECHAT_NAME = inviteName;
      }
      return new Promise(sqlFun);
    }).then((data => {
      var jsonData = { code: 0, msg: "提交成功", data: selfInviteCode };
      res.jsonp(jsonData);
      res.end();
    })).catch((error) => {
      if (error) {
        console.log(error);
        var jsonData = { code: 1, msg: "服务器异常" };
        res.jsonp(jsonData);
        res.end();
      } else {
        return;
      }
    })
  } else {
    var jsonData = { code: 1, msg: "参数缺失" };
    res.jsonp(jsonData);
    res.end();
  }
});


/* GET users listing. */
router.post('/invite/list', function (req, res, next) {
  var code = null;
  var eth = req.body.eth;
  if (eth) {
    var querySql = "select INVITE_CODE from base_invite where ETH_ADDRESS ='" + eth + "'";
    var queryFun = (resolve, reject) => {
      pool.query(querySql, function (error, results, fields) {
        if (error) reject(error);
        if (results && results[0]) {
          resolve(results[0].INVITE_CODE);
        } else {
          reject(null);
        }
      });
    }
    var resultqueryFun = (resolve, reject) => {
      var resultquerySql = "select * from base_invite where PARENT_INVITE_CODE ='" + code + "'";
      pool.query(resultquerySql, function (error, results, fields) {
        if (error) reject(error);
        if (results) {
          resolve(results);
        } else {
          reject(null);
        }
      });
    }
    new Promise(queryFun).then((data) => {
      code = data;
      return new Promise(resultqueryFun);
    }).then((data) => {
      for(var i=0;i<data.length;i++){
        data[i].AWARD_NUM = inviteAward;
      }
      var jsonData = { code: 0, msg: "", data: data };
      res.jsonp(jsonData);
      res.end();
    }).catch((error) => {
      console.log(error);
      var jsonData = { code: 1, msg: "服务器异常" };
      res.jsonp(jsonData);
      res.end();
    });
  } else {
    var jsonData = { code: 1, msg: "参数缺失" };
    res.jsonp(jsonData);
    res.end();
  }
});


/* GET users listing. */
router.post('/invite/list', function (req, res, next) {
  var code = null;
  var eth = req.body.eth;
  if (eth) {
    var querySql = "select INVITE_CODE from base_invite where ETH_ADDRESS ='" + eth + "'";
    var queryFun = (resolve, reject) => {
      pool.query(querySql, function (error, results, fields) {
        if (error) reject(error);
        if (results && results[0]) {
          resolve(results[0].INVITE_CODE);
        } else {
          reject(null);
        }
      });
    }
    var resultqueryFun = (resolve, reject) => {
      var resultquerySql = "select * from base_invite where PARENT_INVITE_CODE ='" + code + "'";
      pool.query(resultquerySql, function (error, results, fields) {
        if (error) reject(error);
        if (results) {
          resolve(results);
        } else {
          reject(null);
        }
      });
    }
    new Promise(queryFun).then((data) => {
      code = data;
      return new Promise(resultqueryFun);
    }).then((data) => {
      var jsonData = { code: 0, msg: "", data: data };
      res.jsonp(jsonData);
      res.end();
    }).catch((error) => {
      console.log(error);
      var jsonData = { code: 1, msg: "服务器异常" };
      res.jsonp(jsonData);
      res.end();
    });
  } else {
    var jsonData = { code: 1, msg: "参数缺失" };
    res.jsonp(jsonData);
    res.end();
  }
});

module.exports = router;
